#-*-coding:UTF-8-*-


def print_bubble_sort(data, end, width=3, debug=False):
    if not debug:
        return
    ss = []
    for index, a in enumerate(data):
        s = str(a)
        if len(s) < width:
            s = ' ' * (width-len(s)) + s
        ss.append(s)
    print '[ %s ]'%(','.join(ss))


def _bubble_sort_action(data, end, debug=False):
    print_bubble_sort(data, end, debug=debug)
    if end == 0:
        return
    i = 0
    while i < end:
        if data[i+1] < data[i]:
            data[i+1], data[i] = data[i], data[i+1]
        i += 1
    _bubble_sort_action(data, end-1, debug=debug)


def bubble_sort(data, debug=False):
    '''bubble sortalgorithm.    author: zhaoxp
    '''
    assert hasattr(data, '__setitem__')
    _bubble_sort_action(data, len(data)-1, debug=debug)